<!-- 用户登录界面 -->
<template>
  <div id="login">
    <div class="bg"></div>
    <el-row class="main-container">
      <el-col :lg="8" :xs="16" :md="10" :span="10">
        <div class="top">
          <i class="iconfont icon-kaoshi"></i><span class="title">在线考试系统</span>
        </div>
        <div class="bottom">
          <div class="container">
            <p class="title">账号登录</p>
            <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
              <el-form-item label="用户名">
                <el-input v-model.number="formLabelAlign.username" placeholder="请输入用户名"></el-input>
              </el-form-item>
              <el-form-item label="密码">
                <el-input v-model="formLabelAlign.password" placeholder="请输入密码" type='password'></el-input>
              </el-form-item>
              <div class="submit">
                <el-button type="primary" class="row-login" @click="login()">登录</el-button>
              </div>
            </el-form>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row class="footer">
      <el-col>
        <ul class="msg2">
          <li>课程：JavaEE架构程序设计</li>
          <li>小组成员：段信维，徐睿彰，林凡</li>
          <li>指导老师：桂浩</li>
        </ul>

      </el-col>
    </el-row>
<!--    <section class="remind">-->
<!--      <span>管理员账号：9527</span>-->
<!--      <span>教师账号：20081001</span>-->
<!--      <span>密码都是：123456</span>-->
<!--    </section>-->
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: "login",
  data() {
    return {
      role: 2,
      labelPosition: 'left',
      formLabelAlign: {
        username: '9527',
        password: '123'
      }
    }
  },
  methods: {
    //用户登录请求后台处理
    login() {
      console.log("登录操作执行-------");
      this.$axios({
        url: `api/login`, // 与 Spring Security 的默认路径一致
        method: "post",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded" // 指定表单格式
        },
        data: new URLSearchParams({
          username: this.formLabelAlign.username, // 保持键名与后端一致
          password: this.formLabelAlign.password
        }).toString()
      })
        .then(response => {
          console.log('Login successful:', response.data);
          // 获取用户信息
          if(response.data.type!=null) {
            // console.log('来判断吧！');
            switch(response.data.type) {
              case "admin":  //管理员
                console.log('管理员！');
                this.$cookies.set("cname", response.data.name)
                this.$cookies.set("cid", response.data.id)
                this.$cookies.set("role", 0)
                this.$router.push({path: '/index' }) //跳转到首页
                break
              case "teacher": //教师
                console.log('教师！');
                this.$cookies.set("cname", response.data.tname)
                this.$cookies.set("cid", response.data.tno)
                this.$cookies.set("role", 1)
                this.$router.push({path: '/index' }) //跳转到教师用户
                break
              case "student": //学生
                console.log('学生！');
                this.$cookies.set("cname", response.data.sname)
                this.$cookies.set("cid", response.data.sno)
                this.$router.push({path: '/student'})
                break
            }
          }
        })
        .catch(error => {
          console.error("登录失败", error);
          this.$message({
            showClose: true,
            type: "error",
            message: "用户名或者密码错误"
          });
        });
    },

    clickTag(key) {
      this.role = key
    }
  },
  computed: mapState(["userInfo"]),
  mounted() {

  }
}
</script>

<style lang="less" scoped>
.remind {
  border-radius: 4px;
  padding: 10px 20px;
  display: flex;
  position: fixed;
  right: 20px;
  bottom: 50%;
  flex-direction: column;
  color: #606266;
  background-color: #fff;
  border-left: 4px solid #409eff;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
.container {
  margin-bottom: 32px;
}
.container .el-radio-group {
  margin: 30px 0px;
}
a:link {
  color:#ff962a;
  text-decoration:none;
}
#login {
  font-size: 14px;
  color: #000;
  background-color: #fff;
}
#login .bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  overflow-y: auto;
  height: 100%;
  background: url('../../assets/img/loginbg.png')center top / cover no-repeat;
  background-color: #b6bccdd1 !important;
}
#login .main-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
#login .main-container .top {
  margin-top: 100px;
  font-size: 30px;
  color: #ff962a;
  display: flex;
  justify-content: center;
}
#login .top .icon-kaoshi {
  font-size: 80px;
}
#login .top .title {
  margin-top: 20px;
}
#login .bottom {
  display:flex;
  justify-content: center;
  background-color:#fff;
  border-radius: 5px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
#login .bottom .title {
  text-align: center;
  font-size: 30px;
}
.bottom .container .title {
  margin: 30px 0px;;
}
.bottom .submit .row-login {
  width: 100%;
  background-color: #04468b;
  border-color: #04468b;
  margin: 20px 0px 10px 0px;
  padding: 15px 20px;
}
.bottom .submit {
  display: flex;
  justify-content: center;
}
.footer {
  margin-top: 50px;
  text-align: center;
}
.footer .msg1 {
  font-size: 18px;
  color: #fff;
  margin-bottom: 15px;
}
.footer .msg2 {
  font-size: 14px;
  color: #e3e3e3;
  margin-top: 70px;
}
.bottom .options {
  margin-bottom: 40px;
  color: #ff962a;
  display: flex;
  justify-content: space-between;
}
.bottom .options > a {
  color: #ff962a;
}
.bottom .options .register span:nth-child(1) {
  color: #8C8C8C;
}
</style>
